/*
 * @Author: Turump 
 * @Date: 2021-04-02 11:01:40 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-06-03 15:12:03
 * Touch事件通知器
 * 事件名称由节点名+事件名(this.node.name + TouchStart/TouchMove/TouchEnd/)
 */

import EventCenter from "../helper/EventCenter";
import { TaskBase } from "./TaskBase";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu("任务组件/触摸事件发射器")
export default class TouchEventEmitor extends TaskBase {
    @property({ displayName: "重复任务", override: true, visible: false })
    repeatableTask = false;

    @property({ displayName: "任务完成Emit事件", override: true, visible() { return this.withTimer } })
    doneEvent = ""

    @property({ displayName: "发射Move事件", tooltip: "Move事件太多,默认不发" })
    isEmitTouchMove = false;

    @property({ displayName: "定时任务", tooltip: "时间到时,会发出您配置的事件" })
    withTimer = false;
    @property({ displayName: "时长(s)", visible() { return this.withTimer } })
    time = 5;

    @property({ displayName: "只处理一次Touch", tooltip: "完成一次性按钮的功能", visible() { return this.withTimer == false } })
    handleOnce = false;

    @property({ displayName: "带音效" })
    withAudio = false;

    @property({ displayName: "音效", type: cc.AudioClip, visible() { return this.withAudio } })
    audioClip: cc.AudioClip = null;

    private touchingFlag = false;   //正在Touch的标志
    private timeTouched = 0;        //触摸总时长
    private audioId = -1;

    enableTask() {
        super.enableTask();
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }

    pauseTask() {
        super.pauseTask();
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }

    resmueTask() {
        super.resmueTask();
        this.enableTask();
    }

    stopTask() {
        super.stopTask();
        cc.audioEngine.stopEffect(this.audioId);
    }

    touchStart(event: cc.Event.EventTouch) {
        if (event.getID() == 0) {
            this.touchingFlag = true;
            cc.audioEngine.stopEffect(this.audioId)
            if (this.audioId == -1) {
                EventCenter.emit(this.node.name + "TouchStart", event);
                this.audioClip && (this.audioId = cc.audioEngine.playEffect(this.audioClip, true));
            }
        }
    }

    touchMove(event: cc.Event.EventTouch) {
        this.isEmitTouchMove && EventCenter.emit(this.node.name + "TouchMove", event);
    }

    touchEnd(event: cc.Event.EventTouch) {
        if (event == null || event.getID() == 0) {
            EventCenter.emit(this.node.name + "TouchEnd", event);
            this.touchingFlag = false;
            cc.audioEngine.stopEffect(this.audioId);
            this.handleOnce && this.pauseTask();
            this.audioId = -1;
        }

    }

    update(dt) {
        if (this.withTimer == false || this.touchingFlag == false)
            return;
        this.timeTouched += dt;
        if (this.timeTouched >= this.time) {
            cc.log(`${this.name} time done`);
            this.touchEnd(null);
            this.pauseTask();   //? pause够否
            this.emitDoneEvent();
        }
    }
}
